<?php

namespace App\Http\Controllers\Company;

use App\Facades\Style;
use App\Http\Controllers\Company\CommonController;
use App\Models\Company\CompanyBasic;
use App\Models\Company\CompanyPhoto;
use Illuminate\Foundation\Auth\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;


class PhotoController extends CommonController{

    /**
     * 相册首页
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request){
        $userInfo = auth()->guard('company')->user();
        $companyPhoto=CompanyPhoto::where('uid',$userInfo->uid)->orderBy('updated_at', 'desc')->get();
        foreach($companyPhoto as $k=>$v){
            $v->file=asset('upload/companyphoto/'.$v->file);
        }

        return Style::view('company.photo',compact('companyPhoto'));
    }

    /**
     * logo上传
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function photoUpload(Request $request){
        if ($request->hasFile('file')) {
            $file=$request->file('file');
            $entension = strtolower($file -> getClientOriginalExtension());
            //$mimeTye = $file -> getMimeType();
            $clientName = $file -> getClientOriginalName();//客服端文件名
            if ($file->isValid()) {
                if (!in_array($entension, array('jpg', 'gif', 'png'))){
                    return response()->json(['error' => 304, 'info' => "文件类型只能为jpg、gif、png！"]);
                }else{
                    $userInfo = auth()->guard('company')->user();
                    $company_basic=CompanyBasic::where('uid',$userInfo->uid)->orderBy('updatetime', 'desc')->first(['id', 'logo']);
                    if(!$company_basic){
                        return response()->json(['error' => 404, 'info' => "没有找到基本信息！"]);
                    }
                    $fileurl=date("Ym").'/';
                    $filename=time().strtolower(str_random(5)).'.'.$entension;
                    $request->file('file')->move(public_path().'/upload/companyphoto/'.$fileurl,$filename);
                    $companyPhoto = new CompanyPhoto;
                    $companyPhoto->uid = $userInfo->uid;
                    $companyPhoto->com_id = $company_basic->id;
                    $companyPhoto->file = $fileurl.$filename;
                    $companyPhoto->save();

                    return response()->json(['status' => 200,'id'=>$companyPhoto->id,'url' => asset('upload/companyphoto/'.$fileurl.$filename)]);
                }
            }
        }else{
            return response()->json(['error' => 306, 'info' => "没有选择上传图片文件！"]);
        }
    }

    public function photoDelete($id){
        $userInfo = auth()->guard('company')->user();
        $companyPhoto=CompanyPhoto::where('id',$id)->where('uid',$userInfo->uid)->first();
        if(!empty($companyPhoto->file)){
            @unlink(public_path().'/upload/companyphoto/'.$companyPhoto->file);
            if(CompanyPhoto::where('id', $companyPhoto->id)->delete()){
                return response()->json(['status' => 200]);
            }
        }
    }
}